//
//  UIButton+Common.m
//  Logistics
//
//  Created by 刘峰 on 2018/4/25.
//  Copyright © 2018年 com.Logistics. All rights reserved.
//

#import "UIButton+Common.h"

@implementation UIButton (Common)

- (void)rightImageEdge {
    if (@available(iOS 9.0, *)) {
        self.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        self.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    } else {
        // Fallback on earlier versions
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.width, 0, self.imageView.width);
        self.imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.width+2.5, 0, -self.titleLabel.width-2.5);
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
}

- (void)openTimer:(int)time Title:(NSString *)title {
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    WEAKSELF
    __block int timeOut = time;
    dispatch_source_set_event_handler(timer, ^{
        
        timeOut --;
        
        if (timeOut <= 0) {
            dispatch_source_cancel(timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.userInteractionEnabled = YES;
                [weakSelf setTitle:title forState:UIControlStateNormal];
            });
        }else {

            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.userInteractionEnabled = NO;
                [weakSelf setTitle:[NSString stringWithFormat:@"%dS重新发送", timeOut] forState:UIControlStateNormal];
            });
            
        }
    });
    
    dispatch_resume(timer);
}




@end
